<template>
  <div>
    <div class="header">
      <el-input placeholder="请输入内容"></el-input>
      <el-input placeholder="请输入内容"></el-input>
      <el-button type="primary">
        <svg-icon icon-class="search" />
        搜索
      </el-button>
      <el-button type="primary">
        <svg-icon icon-class="edit" />
        添加
      </el-button>
      <el-button type="primary" @click="excelData">
        <svg-icon icon-class="form" />
        导出
      </el-button>
    </div>

    <div class="table">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" class="demo-table-expand">
              <el-form-item label="宣传画廊">
                <div class="label_expand_img">
                  <img
                    v-for="iten in props.row.gallery"
                    :key="iten"
                    :src="iten"
                    alt=""
                  />
                </div>
              </el-form-item>
              <el-form-item label="商品介绍">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="商品单位">
                <span>{{ props.row.unit }}</span>
              </el-form-item>
              <el-form-item label="关键字">
                <span>{{ props.row.keywords }}</span>
              </el-form-item>
              <el-form-item label="类目id">
                <span>{{ props.row.categoryId }}</span>
              </el-form-item>
              <el-form-item label="品牌商id">
                <span>{{ props.row.brandId }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="商品编号" width="100">
        </el-table-column>
        <el-table-column prop="name" width="120" label="名称">
        </el-table-column>
        <el-table-column width="100" label="图片">
          <template slot-scope="scope">
            <img :src="scope.row.picUrl" alt="" />
          </template>
        </el-table-column>
        <el-table-column width="100" label="分享图"> </el-table-column>
        <el-table-column width="100" label="详情">
          <template slot-scope="scope">
            <el-button type="primary" @click="detailclick(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="counterPrice" width="100" label="专柜价格">
        </el-table-column>
        <el-table-column prop="retailPrice" width="100" label="当前价格">
        </el-table-column>
        <el-table-column width="100" label="是否新品">
          <template slot-scope="scope">
            <span class="isnew">{{ scope.row.isNew ? "新品" : "非新品" }}</span>
          </template>
        </el-table-column>
        <el-table-column width="100" label="是否热品">
          <template slot-scope="scope">
            <span class="isnew">{{ scope.row.isHot ? "热品" : "非热品" }}</span>
          </template>
        </el-table-column>
        <el-table-column class="a" width="100" label="是否在售">
          <template slot-scope="scope">
            <span class="isOnSale">
              {{ scope.row.isOnSale ? "在售" : "非在售" }}
            </span>
          </template>
        </el-table-column>
        <el-table-column border fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="editclick(scope.row)"
              type="primary"
              size="small"
            >
              编辑
            </el-button>
            <el-button @click="delclick(scope.row)" type="danger" size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :visible.sync="dialogVisible">
      <div v-html="detail"></div>
    </el-dialog>
    <div style="padding: 40px 0" class="footer">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getgoodslist } from "@/api/goods";
export default {
  name: "",
  props: [],
  data() {
    return {
      page: 1, //页数
      limit: 10, //一页多少条
      tableData: [], //table数据
      total: 0,
      dialogVisible: false,
      detail: "",
    };
  },
  mounted() {
    this.gettabledata();
  },
  methods: {
    gettabledata() {
      getgoodslist({ page: this.page, limit: this.limit }).then((res) => {
        console.log(res);
        if (res.errno == 0) {
          this.tableData = res.data.items;
          this.total = res.data.total;
        }
      });
    },
    //点击详情
    detailclick(e) {
      console.log(e);
      this.detail = e.detail.replace(/\<img/gi, '<img class="richimg"');
      this.dialogVisible = true;
    },
    // 改变每一页多少条数据
    handleSizeChange(e) {
      this.limit = e;

      this.gettabledata();
    },
    //改变当前页数
    handleCurrentChange(e) {
      this.page = e;
      this.gettabledata();
    },

    //导出excel表格
    excelData() {
      const headers = {
        商品编号: "id",
        名称: "name",
        图片: "picUrl",
        专柜价格: "counterPrice",
        当前价格: "retailPrice",
        是否新品: "isNew",
        是否热品: "isHot",
        是否在售: "isOnSale",
      };
      //懒加载
      import("@/vendor/Export2Excel").then((excel) => {
        const data = this.formatJson(headers, this.tableData);
        excel.export_json_to_excel({
          header: Object.keys(headers),
          data,
          filename: "商品列表",
        });
      });
    },
    // 该方法负责将数组转化成二维数组
    formatJson(headers, rows) {
      //首先遍历数组
      return rows.map((item) => {
        // item是对象  => 转化成只有值的数组 => 数组值的顺序依赖headers  {username: '张三'  }
        // Object.keys(headers)  => ["姓名", "手机号",...]
        return Object.keys(headers).map((key) => {
          return item[headers[key]]; // 得到 ['张三'，’129‘，’dd‘,'dd']
        });
      });
    },
  },

  computed: {},
};
</script>

<style lang="less" scoped>
.header {
  padding: 15px;
  display: flex;
  flex-wrap: wrap;
  /deep/.el-input {
    width: 200px;
    margin-left: 15px;
    .el-input__inner {
      width: 200px;
    }
  }
  .el-button {
    margin-left: 15px;
  }
}
/deep/.el-dialog__body {
  .richimg {
    width: 100% !important;
  }
}

.table {
  padding: 15px;
  /deep/.cell {
    text-align: center;
    .iconfont {
      padding: 5px;
    }
    img {
      width: 40px;
      height: 40px;
    }
    .isnew {
      display: inline-block;
      padding: 3px 8px;
      border: 1px solid #409efe;
      border-radius: 5px;
      color: #409efe;
      background-color: #e3eefb;
    }
    .isOnSale {
      display: inline-block;
      padding: 3px 8px;
      background-color: #e7f2e7;
      color: #67c23a;
      border: 1px solid #67c23a;
      border-radius: 5px;
    }
  }
}
.label_expand_img {
  img {
    width: 100px;
    height: 100px;
    margin-left: 20px !important;
  }
}
</style>
